package org.example.MysqlToHive;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.prefs.Preferences;


public class SimpleForm extends JFrame {
    private JTextField ipTextField;
    private JTextField portTextField;
    private JTextField usernameTextField;
    private JPasswordField passwordField;
    private JTextField tableNameTextField;
    private JTextField databaseNameTextField;
    private Preferences prefs;
    private JTextArea outputTextArea;

    public SimpleForm() {
        setTitle("数据库连接输入");
        setSize(400, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        prefs = Preferences.userRoot().node(this.getClass().getName());

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(8, 2));

        JLabel ipLabel = new JLabel("主机IP:");
        ipTextField = new JTextField(prefs.get("ip", ""));

        JLabel portLabel = new JLabel("端口号:");
        portTextField = new JTextField(prefs.get("port", ""));

        JLabel usernameLabel = new JLabel("用户名:");
        usernameTextField = new JTextField(prefs.get("username", ""));

        JLabel passwordLabel = new JLabel("密码:");
        passwordField = new JPasswordField(prefs.get("password", ""));

        JLabel tableNameLabel = new JLabel("表名:");
        tableNameTextField = new JTextField(prefs.get("tableName", ""));

        JLabel databaseNameLabel = new JLabel("库名:");
        databaseNameTextField = new JTextField(prefs.get("databaseName", ""));

        JButton submitButton = new JButton("提交");

        JLabel outputLabel = new JLabel("输出:");
        outputTextArea = new JTextArea();
//        outputTextArea.set
//        outputTextArea.setEditable(false);
        submitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String ip = ipTextField.getText();
                int port = Integer.parseInt(portTextField.getText());
                String username = usernameTextField.getText();
                String password = new String(passwordField.getPassword());
                String tableName = tableNameTextField.getText();
                String databaseName = databaseNameTextField.getText();

                // 保存输入信息
                prefs.put("ip", ip);
                prefs.put("port", String.valueOf(port));
                prefs.put("username", username);
                prefs.put("password", password);
                prefs.put("tableName", tableName);
                prefs.put("databaseName", databaseName);

                JOptionPane.showMessageDialog(null, "输入信息已接收，可进行数据库连接操作");
                String hiveDDL = new MysqlToHive_V().getHiveDDL(ip, port, username, password, databaseName, tableName);
                outputTextArea.setText(hiveDDL);
            }
        });

        panel.add(ipLabel);
        panel.add(ipTextField);
        panel.add(portLabel);
        panel.add(portTextField);
        panel.add(usernameLabel);
        panel.add(usernameTextField);
        panel.add(passwordLabel);
        panel.add(passwordField);
        panel.add(tableNameLabel);
        panel.add(tableNameTextField);
        panel.add(databaseNameLabel);
        panel.add(databaseNameTextField);
        panel.add(outputLabel);
        panel.add(outputTextArea);
        panel.add(new JLabel());
        panel.add(submitButton);

        add(panel);
        setVisible(true);
    }

    public static void main(String[] args) {
        new SimpleForm();
    }
}